import datetime
from lunar_python import Lunar
import json


class XLR:

    # 实例创建（待开发：内置参数类型错误处理机制）
    def __init__(self, a, b, c, ShiChen="now"):

        self.a = a
        self.b = b
        self.c = c

        if ShiChen == "now":
            self.ShiChen = self.Now_ShiChen()
        else:
            self.ShiChen = ShiChen

    # 获取取数
    def QuShu(self):
        return self.a, self.b, self.c

    # 获取当前时辰
    def Now_ShiChen(self):
        d = Lunar.fromDate(datetime.datetime.now())
        return d.getTimeInGanZhi()[1]

    # 获取六神
    def LiuShen(self):
        list_LiuShen = ["大安", "留连", "速喜", "赤口", "小吉", "空亡"]

        # 获取六神索引
        def calculate(a, b, c):
            Tian = (a % 6) - 1
            Di = (a % 6 - 1 + b) % 6 - 1
            Ren = ((a % 6 - 1 + b) % 6 - 1 + c) % 6 - 1
            return Tian, Di, Ren

        self.Tian, self.Di, self.Ren = calculate(self.a, self.b, self.c)

        return list_LiuShen[self.Tian], list_LiuShen[self.Di], list_LiuShen[self.Ren]

    # 获取五行
    def WuXing(self):
        list_WuXing = ["木", "土", "火", "金", "水", "土"]
        return list_WuXing[self.Tian], list_WuXing[self.Di], list_WuXing[self.Ren]

    # 获取六亲
    def LiuQin(self):
        list_LiuQin = ["兄弟", "子孙", "妻财", "官鬼", "父母"]
        list_WuXing = ["木", "火", "土", "金", "水"]

        # 获取六亲索引
        def LiuQin_Index(Other, Ren):
            id_Other = list_WuXing.index(Other)
            id_Ren = list_WuXing.index(Ren)

            if id_Other == id_Ren:
                return list_LiuQin[0]
            elif (id_Other - 1) % 5 == id_Ren:
                return list_LiuQin[1]
            elif (id_Other + 1) % 5 == id_Ren:
                return list_LiuQin[4]
            elif (id_Other + 2) % 5 == id_Ren:
                return list_LiuQin[3]
            elif (id_Other + 3) % 5 == id_Ren:
                return list_LiuQin[2]

        a, b, c = self.WuXing()
        return LiuQin_Index(a, c), LiuQin_Index(b, c), "本元"

    # 获取邵氏神煞
    def ShaoShi_ShenSha(self):
        list_ShaoShi_ShenSha = ["青龙", "腾蛇", "朱雀", "白虎", "玄武", "勾陈"]
        return (
            list_ShaoShi_ShenSha[self.Tian],
            list_ShaoShi_ShenSha[self.Di],
            list_ShaoShi_ShenSha[self.Ren],
        )

    # 获取动宫
    def DongGong(self):
        list_DongGong = ["事业", "田宅", "感情", "疾厄", "驿马", "福德"]
        return list_DongGong[self.Tian], list_DongGong[self.Di], list_DongGong[self.Ren]

    # 获取静宫
    def JingGong(self):
        list_JingGong = ["命宫", "奴仆", "夫妻", "兄弟", "子女", "父母"]
        return list_JingGong[self.Tian], list_JingGong[self.Di], list_JingGong[self.Ren]

    # 获取全部排盘（英文键名）
    def en_All(self):
        return {
            "QuShu": self.QuShu(),
            "LiuShen": self.LiuShen(),
            "WuXing": self.WuXing(),
            "LiuQin": self.LiuQin(),
            "ShaoShi_ShenSha": self.ShaoShi_ShenSha(),
            "DongGong": self.DongGong(),
            "JingGong": self.JingGong(),
            "ShiChen": self.Now_ShiChen(),
        }

    # 获取全部排盘（中文键名）
    def ch_All(self):
        return {
            "取数": self.QuShu(),
            "六神": self.LiuShen(),
            "五行": self.WuXing(),
            "六亲": self.LiuQin(),
            "神煞": self.ShaoShi_ShenSha(),
            "动宫": self.DongGong(),
            "静宫": self.JingGong(),
            "时辰": self.Now_ShiChen(),
        }

    # 获取全部排盘（英文键名，JSON格式）
    def json_en_All(self):
        return json.dumps(self.en_All(), indent=4, ensure_ascii=False)

    # 获取全部排盘（中文键名，JSON格式）
    def json_ch_All(self):
        return json.dumps(self.ch_All(), indent=4, ensure_ascii=False)


"""

# 测试

xlr = XLR(1, 2, 3)      # 创建实例
result = xlr.ch_All()   # 调用方法
print(result)           # 输出结果


"""
